package org.jsrml.sv.mob.controller.base;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.jsrml.common.pagination.Pagination;
import org.jsrml.common.util.JSONResultDTO;
import org.jsrml.core.base.command.DeleteResourceCo;
import org.jsrml.core.base.command.SaveResourceCo;
import org.jsrml.core.base.entity.Resource;
import org.jsrml.core.base.qo.ResourceQO;
import org.jsrml.core.base.service.ResourceService;
import org.jsrml.sv.mob.controller.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(value = "/resource")
public class ResourceController extends BaseController {

	private static Logger logger = LoggerFactory
			.getLogger(ResourceController.class);

	@Autowired
	private ResourceService resourceService;

	@RequestMapping("/query")
	@ResponseBody
	public JSONResultDTO query(HttpServletRequest request, @RequestBody ResourceQO qo) {
		try {
			switch (qo.getResultType()) {
			case 0:
				Integer count = resourceService.queryCount(qo);
				return createSuccessResult(count);
			case 1:
				Resource resource = resourceService.queryUnique(qo);
				return createSuccessResult(resource);
			case 2:
				List<Resource> list = resourceService.queryList(qo);
				return createSuccessResult(list);
			case 3:
				Pagination pagination = resourceService.queryPagination(qo);
				return createSuccessResult(pagination);
			default:
				return createFailResult("-1", "参数中缺少正确的resultType", null);
			}
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
	}
	
	/**
	 * 删除资源
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public JSONResultDTO delete(HttpServletRequest request,
			@RequestBody DeleteResourceCo deleteCommand) {
		try {
			resourceService.deleteResource(deleteCommand);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(null);
	}

	/**
	 * 创建资源
	 */
	@RequestMapping("/create")
	@ResponseBody
	public JSONResultDTO regist(HttpServletRequest request,
			@RequestBody SaveResourceCo createCommand) {
		Resource resource = null;
		try{
			resource = resourceService.createResource(createCommand);
		}catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(resource);
	}

	/**
	 * 修改资源
	 */
	@RequestMapping("/modify")
	@ResponseBody
	public JSONResultDTO modify(HttpServletRequest request,
			@RequestBody SaveResourceCo modifyCommand) {

		Resource resource = null;
		try{
			resource = resourceService.modifyResource(modifyCommand);
		}catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(resource);
	}
}
